package com.forsythia.sns.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import com.forsythia.sns.ValidateMessage;
import com.forsythia.sns.dao.UserDAO;
import com.forsythia.sns.pagination.PaginationUtils;
import com.forsythia.sns.to.FriendTO;
import com.forsythia.sns.to.UserTO;

public class UserServiceImpl extends BaseServiceImpl implements UserService {
    @Override
    public boolean isLoginNameExists(String loginName) {
        return userDAO.isLoginNameExists(loginName);
    }

    @Override
    public boolean isEmailExists(String email) {
        return userDAO.isEmailExists(email);
    }
    
    @Override
    public boolean isPassportExists(String passport) {
        return userDAO.isEmailExists(passport) || userDAO.isLoginNameExists(passport);
    }

    @Override
    public UserTO createUserAccount(UserTO user) {
        return userDAO.insert(user);
    }

    @Override
    public List<UserTO> findByProperty(String propertyName, Object value) {
        return userDAO.findByProperty(UserTO.class, propertyName, value);
    }

    // /////////////////////////////////////////////////////////////////////////
    private UserDAO userDAO;

    public UserDAO getUserDAO() {
        return userDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    // /////////////////////////////////////////////////////////////////////////
    @Override
    public boolean isLoginSuccess(String passport, String password) {
        Validate.notEmpty( passport, ValidateMessage.notEmpty("UserServiceImpl", "isLoginSuccess", "passport"));
        Validate.notEmpty( password, ValidateMessage.notEmpty("UserServiceImpl", "isLoginSuccess", "password"));
        
        List<UserTO> userList = userDAO.findByProperty(UserTO.class, "email", passport);
        userList.addAll(userDAO.findByProperty(UserTO.class, "loginName", passport));
        for(int i=0; i < userList.size(); i++){
            if(password.equals(userList.get(i).getPassword())){
                return true;
            }
        }
        return false;
    }

    @Override
    public UserTO getUserByPassport(String passport, String password) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<UserTO> getFriends(String userId, int pageNumber, int pageSize){
        return userDAO.getFriends(userId, PaginationUtils.getFirstResult(pageNumber, pageSize), pageSize);
    }

    @Override
    public FriendTO addFriend(FriendTO friend) {
        return userDAO.insert(friend);
    }
    
    



}
